<?

class db
{
var $dbconn;			// database connection
var $rows_per_page;     // used in pagination
var $pageno;            // current page number
var $pagecount;          // highest page number
var $row;        		// data from the database
var $errors;            // array of error messages
var $sql;				// sql query string
var $numrows;			// total no. of rows in result set
var $countno;			

function db()
{
	$this->rows_per_page	= PAGINGCOUNT;
	$this->db_connect();
}//construct

function db_connect_mhb()
{

   if (!$this->dbconn)
   		$this->dbconn = mysql_connect(SVRNAME, USRNAME2, DBPWD2);

   if (!$this->dbconn) {
      return 0;
   } elseif (!mysql_select_db(DBNAME2)) {
      return 0;
   } else {
      return $this->dbconn;
   } // if

} // db_connect


function db_connect()
{

   if (!$this->dbconn)
   		$this->dbconn = mysql_connect(SVRNAME, USRNAME, DBPWD);

   if (!$this->dbconn) {
      return 0;
   } elseif (!mysql_select_db(DBNAME)) {
      return 0;
   } else {
      return $this->dbconn;
   } // if

} // db_connect

function get_insert_id()
{
	return mysql_insert_id();
}

function db_close()
{
	mysql_close($this->dbconn);
}

function get_numrows($sql)
{
	$result = mysql_query($sql) or die(mysql_error());

	$this->numrows = mysql_num_rows($result);
	return $this->numrows;
}

function get_count($sql)
{
	$result = mysql_query($sql) or die(mysql_error());
	$this->countno = mysql_result($result,0,"total");
	return $this->countno;
}

function set_sql($sql)
{
	$this->sql	= $sql;
}

function set_rows_per_page($num)
{
	$this->rows_per_page	= $num;
}

function set_pageno($num)
{
	$this->pageno	= $num;
}

function get_pagecount()
{
	return $this->pagecount;
}

/**********************************************/
/* Get Data for Paging */
/**********************************************/
function get_datalist($sql,$desired_num_rows = false)
{
	$this->row 		= array();
	$pageno         = $this->pageno;
	$this->numrows  = 0;
	$this->pagecount = 0;

	if ($desired_num_rows)
		$rows_per_page	= $desired_num_rows;
	else
		$rows_per_page  = $this->rows_per_page;

	$numrows		= $this->get_numrows($sql);

	if ($numrows <= 0) {
		$this->pageno = 0;
		return;
	} // if

	if ($rows_per_page > 0) {
		$this->pagecount = ceil($numrows / $rows_per_page);
	} else {
		$this->pagecount = 1;
	} // if

	if ($pageno == '' OR $pageno <= '1') {
		$pageno = 1;
	} elseif ($pageno > $this->pagecount) {
		$pageno = $this->pagecount;
		
	} // if

	$this->pageno = $pageno;

	if ($rows_per_page > 0) {
		$limit_str = ' LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
	} else {
		$limit_str = NULL;
	} // if

	$sql .= $limit_str;

	$result = mysql_query($sql) or die(mysql_error());

	while ($row = mysql_fetch_array($result)) {
		$this->row[] = $row;
	} // while

	mysql_free_result($result);

	return $this->row;
} // getData

/**********************************************/
/* Execute Data Manipulation Language Upon Table */
/**********************************************/
function execute_nonquery($sql)
{
	$result	= mysql_query($sql) or die(mysql_error());

	return $result;
}//execute_cmd

/**********************************************/
/* Get Specific Data from Table */
/**********************************************/
function execute_query($sql)
{
	$result	= mysql_query($sql) or die(mysql_error());
	$row 	= mysql_fetch_array($result);
	mysql_free_result($result);

	return $row;
}//get_resultset

/**********************************************/
/* Get All Data from Table */
/**********************************************/
function get_all_data($sql)
{
	$result = mysql_query($sql) or die(mysql_error());

	while ($row = mysql_fetch_array($result)) {
		$this->row[] = $row;
	} // while

	mysql_free_result($result);

	return $this->row;
}//get_all_data

}//class
?>